﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PMS.ENT;
using PMS.DAL;

namespace PMS.BLL
{
    public class NotificationBLL
    {

        private static UserGroupCollection _newuser;

        public static UserGroupCollection NewUser
        {
            get { return _newuser; }
            set { _newuser = value; }
        }

        #region Get
        public static NotificationENT GetNotificationByID(int NotificationID)
        {
            try
            {
                return NotificationDAL.GetNotificationByID(NotificationID);
            }
            catch (Exception)
            {
                throw;
            }
        }


        public static NotificationCollection GetNotificationListByUserID(int userId, string orderBy, string orderDirection, int page, int pageSize, out int totalRecords)
        {
            try
            {
                return NotificationDAL.GetNotificationListByUserID(userId, orderBy, orderDirection, page, pageSize, out totalRecords);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static int GetNotificationUnreadNumber(int userID)
        {
            try
            {
                return NotificationDAL.GetNotificationUnreadNumber(userID);
            }
            catch (Exception)
            {

                throw;
            }
        }



        #endregion

        #region Insert Update Delete Confirm

        public static NotificationCollection InsertNotification(int userID, NotificationType notificationType, string role, int groupID, string url, int projectID, int scheduleItemID)
        {
            try
            {
                //Get group memberp
                GroupENT group = GroupBLL.GetGroupByID(Convert.ToInt32(groupID));
                group.ListGroupMember = UserGroupBLL.GetUserGroupListByGroupID(Convert.ToInt32(groupID));
                //Get Project Info
                ProjectENT project = ProjectBLL.GetProjectByID(projectID);
                ScheduleItemENT scheduleItem = ScheduleItemBLL.GetScheduleItemByID(scheduleItemID);
                //get user info
                UserENT user = UserBLL.GetUserByID(userID);
                NotificationCollection listNotification = new NotificationCollection();
                //Description
                string desctiption = string.Empty;

                if (role == Role.Teacher.ToString())
                {
                    desctiption += "Giảng viên " + user.FullName;
                }
                else
                {
                    desctiption += "Sinh viên '" + user.FullName + "'";
                }

                if (notificationType == NotificationType.Comment)
                {
                    desctiption += " bình luận về công việc '" + scheduleItem.Work + "' đồ án '" + project.ProjectName + "' của nhóm '" + group.GroupName + "'";
                }
                else if (notificationType == NotificationType.AddSchedule)
                {
                    desctiption += " đã xếp lịch thực hiện đồ án '" + project.ProjectName + "' cho nhóm '" + group.GroupName + "'";
                }
                else if (notificationType == NotificationType.ConfirmGroup)
                {
                    desctiption += " đã chấp nhận tham gia nhóm '" + group.GroupName + "'";
                }
                else if (notificationType == NotificationType.InviteGroup || notificationType == NotificationType.InviteGroupMore)
                {
                    desctiption += " mời bạn tham gia nhóm '" + group.GroupName + "'";
                }
                else if (notificationType == NotificationType.JoinProject)
                {
                    desctiption += " đã đăng ký tham gia đề tài '" + project.ProjectName + "'";
                }
                else if (notificationType == NotificationType.Mark)
                {
                    desctiption += " đã chấm điểm cho công việc '" + scheduleItem.Work + "' đề tài '" + project.ProjectName + "' của nhóm '" + group.GroupName + "'";
                }
                else if (notificationType == NotificationType.Upload)
                {
                    desctiption += " đã nộp bài cho công việc '" + scheduleItem.Work + "' đề tài '" + project.ProjectName + "' của nhóm '" + group.GroupName + "'";
                }

                if (role != Role.Teacher.ToString()
                    && notificationType != NotificationType.InviteGroup
                    && notificationType != NotificationType.ConfirmGroup
                    )
                {
                    int teacherid = 0;

                    if (notificationType != NotificationType.JoinProject)
                        teacherid = scheduleItem.CreatedBy;
                    else
                        teacherid = project.OwnerID;

                    NotificationENT noteacher = new NotificationENT
                    {
                        CreatedBy = userID,
                        Description = desctiption,
                        GroupID = 0,
                        IsDelete = 0,
                        IsRead = 0,
                        Link = url,
                        UserID = teacherid
                    };

                    listNotification.Add(noteacher);
                }

                if (notificationType != NotificationType.InviteGroupMore)
                {
                    foreach (var student in group.ListGroupMember)
                    {
                        if (student.UserID != userID)
                        {
                            NotificationENT nostudent = new NotificationENT
                            {
                                CreatedBy = userID,
                                Description = desctiption,
                                GroupID = student.GroupID,
                                IsDelete = 0,
                                IsRead = 0,
                                Link = url,
                                UserID = student.UserID
                            };
                            listNotification.Add(nostudent);
                        }
                    }
                }
                else
                {
                    foreach (var student in group.ListGroupMember)
                    {
                        if (_newuser.Where(d => d.UserID == student.UserID).Count() > 0)
                        {
                            NotificationENT nostudent = new NotificationENT
                            {
                                CreatedBy = userID,
                                Description = desctiption,
                                GroupID = student.GroupID,
                                IsDelete = 0,
                                IsRead = 0,
                                Link = url,
                                UserID = student.UserID
                            };
                            listNotification.Add(nostudent);
                        }
                    }
                }

                if (listNotification.Count > 0)
                {
                    InsertNotification(listNotification);
                }
                return listNotification;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public static int InsertNotification(NotificationENT Notification)
        {
            try
            {
                return NotificationDAL.InsertNotification(Notification);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public static bool InsertNotification(NotificationCollection listNotification)
        {
            try
            {
                return NotificationDAL.InsertNotification(listNotification);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public static int UpdateNotificationIsRead(int notificationID)
        {
            try
            {
                return NotificationDAL.UpdateNotificationIsRead(notificationID);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public static int DeleteNotification(string strNotificationID)
        {
            try
            {
                return NotificationDAL.DeleteNotification(strNotificationID);
            }
            catch (Exception)
            {

                throw;
            }
        }




        #endregion


    }
}
